<template>
    <div class="aside" :style="{ width: isMenuOpen ? '240px' : '130px' }">
        <div class="navigation">
            <div class="menuToggle"></div>
            <ul>
                <li class="list" v-for="(item, index) in MenuData" :key="index"
                    :class="{ 'active': index === activeIndex }" @click="clickItem(item)">
                    <a href="#" :style="{ '--clr': item.color }">
                        <span class="icon" :class="item.icon">
                            <i class="iconfont" :class="item.icon"></i>
                        </span>
                        <span class="text">{{ item.text }}</span>
                    </a>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import cookie from 'js-cookie'
export default {
    data() {
        return {
            activeIndex: 0, // 默认活动项的索引
            isMenuOpen: false, // 默认菜单关闭状态
        };
    },
    methods: {
        clickItem(item) {
            // 防止自己跳自己的报错
            if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
                this.$router.push(item.path)
            }
            // 面包屑
            this.$store.commit('SelectMenu', item)
        },
        toggleMenu() {
            this.isMenuOpen = !this.isMenuOpen; // 切换菜单状态
        },
    },
    computed: {
        // 获取菜单
        MenuData() {
            return JSON.parse(cookie.get('menu')) || this.$store.state.tab.menu
        }
    },
    mounted() {
        document.querySelector('.menuToggle').addEventListener('click', function () {
            document.querySelector('.navigation').classList.toggle('active');
        });

        const listItems = document.querySelectorAll('.navigation .list');
        listItems.forEach(item => {
            item.addEventListener('click', function (event) {
                event.preventDefault();
                listItems.forEach(listItem => listItem.classList.remove('active'));
                this.classList.add('active');
            });
        });

        document.querySelector('.menuToggle').addEventListener('click', () => {
            this.toggleMenu(); 
        });
    }
}
</script>

<style scoped>
.navigation {
    position: fixed;
    inset: 40px 0 40px 20px;
    background: #fff;
    width: 75px;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
    transition: 0.5s;
    color: #fff;
    z-index: 99;
}

.navigation.active {
    width: 189px;
}

.menuToggle {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    padding: 0 25px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.25);
    display: flex;
    justify-content: flex-start;
    align-items: center;
    cursor: pointer;
}

.menuToggle::before {
    content: '';
    position: absolute;
    width: 30px;
    height: 2px;
    background: #333;
    transform: translateY(-8px);
    transition: .5s;
}

.navigation.active .menuToggle::before {
    transform: translateY(0) rotate(45deg);
}

.aside {
    width: 130px;
    height: 100%;
    background-color: #2f323f;
    transition: width 0.5s;
}

.menuToggle::after {
    content: '';
    position: absolute;
    width: 30px;
    height: 2px;
    background: #333;
    transform: translateY(8px);
    box-shadow: 0 -8px 0 #333;
    transition: .5s;
}

.navigation.active .menuToggle::after {
    transform: translateY(0) rotate(-45deg);
    box-shadow: 0 0 0 #333;
}

.navigation ul {
    display: flex;
    flex-direction: column;
    width: 100%;
}

.navigation ul li {
    list-style: none;
    position: relative;
    width: 100%;
    height: 76px;
    border-radius: 12px;
    border: 8px solid transparent;
    transition: .5s;
}

.navigation ul li.active {
    transform: translateX(30px);
    background: #2f323f;
}

.navigation ul li::before {
    content: '';
    position: absolute;
    top: -28px;
    right: -10px;
    width: 20px;
    height: 20px;
    background: transparent;
    border-bottom-right-radius: 20px;
    box-shadow: 6px 5px 0 5px #2f323f;
    transform: scale(0);
    transform-origin: bottom right;
    transition: .5s;
}

.navigation ul li.active::before {
    right: 22px;
    transform: scale(1);
}

.navigation ul li::after {
    content: '';
    position: absolute;
    bottom: -28px;
    right: -10px;
    width: 20px;
    height: 20px;
    background: transparent;
    border-top-right-radius: 20px;
    box-shadow: 6px -3px 0 3px #2f323f;
    transform: scale(0);
    transform-origin: top right;
    transition: .5s;
}

.navigation ul li.active::after {
    right: 22px;
    transform: scale(1);
}

.navigation ul li a {
    position: relative;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    width: 100%;
    text-align: center;
    text-decoration: none;
    z-index: 1000;
}

.navigation ul li a .icon {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 60px;
    height: 60px;
    border-radius: 10px;
    font-size: 1.75em;
    line-height: 60px;
    border: 6px solid transparent;
    transition: .5s;
}

.navigation ul li a .icon i {
    font-size: 24px;
}

.navigation ul li.active a .icon {
    color: #fff;
    background: var(--clr);
}

.navigation ul li a .icon::before {
    content: '';
    position: absolute;
    top: 12px;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--clr);
    filter: blur(8px);
    opacity: 0;
    transition: 0.5s;
}

.navigation ul li.active a .icon::before {
    opacity: .5;
}

.navigation ul li a .icon::after {
    content: '';
    position: absolute;
    top: 10px;
    left: -62px;
    width: 15px;
    height: 15px;
    background: var(--clr);
    border: 8px solid #2f323f;
    border-radius: 50%;
}

.navigation ul li a .text {
    position: relative;
    padding: 0 15px;
    color: #333;
    display: flex;
    align-items: center;
    height: 60px;
    opacity: 0;
    visibility: hidden;
    transition: .5s;
}

.navigation.active ul li a .text {
    opacity: 1;
    visibility: visible;
}

.navigation ul li.active a .text {
    color: #fff;
}

.iconfont {
    color: black;
}
</style>